<?php
	/**
	 * Modelo de ficheiro Excel para exportação de Projectos
	 * Precedencias:
	 * 		=> DIR_PROJECTOS/exportar_excel_projectos.php
	 * 
	 * @since 02/04/2008
	 * @author JRS
	 */
	
	#Includes SoftsetSense
	require_once ('../includes/config.php');
	include DIR_INCLUDES_FT.'bootstrap.php';
	require_once (DIR_TOOLKIT_FT.'phpExcel/EXCELExport.class.php');
	
	#Includes PHPExcel
	require_once (DIR_TOOLKIT_FT.'phpExcel/PHPExcel.php');
	require_once (DIR_TOOLKIT_FT.'phpExcel/PHPExcel/RichText.php');

class ExportarExcelLoja extends EXCELExport {
	
	/**
	 * Método Constructor
	 *
	 * @param unknown_type $projectoID
	 */
	public function __construct($loja_id) {
		$this->loja_id = $loja_id;
		parent::__construct($this->geraExcel(), $this->getNomeFicheiro());
	}
	
	/*************************************************************************
	 ************************* INICIO DO DOCUMENTO ***************************/
	public function geraExcel () {
		
		ini_set("memory_limit","256M");
		
		$estiloCategorias = array(
			'font' => array(
				'size' => 10,
				'bold' => true
			),
			'alignment' => array(
				'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER
			),
			'borders' => array(
				'bottom' => array(
					'style' => PHPExcel_Style_Border::BORDER_MEDIUM
				),
				'top' => array(
					'style' => PHPExcel_Style_Border::BORDER_MEDIUM
				),
				'left' => array(
					'style' => PHPExcel_Style_Border::BORDER_MEDIUM
				),
				'right' => array(
					'style' => PHPExcel_Style_Border::BORDER_MEDIUM
				)
			),
			'fill' => array(
	 			'type'       => PHPExcel_Style_Fill::FILL_SOLID,
	  			'rotation'   => 90,
	 			'startcolor' => array(
	 				'argb' => 'FFFFFF00'
	 			),
	 			'endcolor'   => array(
	 				'argb' => 'FFFFFF00'
	 			)
	 		)
		);
		
		$estiloNormal = array(
			'font' => array(
				'size' => 10,
				'bold' => true
			),
			'borders' => array(
				'bottom' => array(
					'style' => PHPExcel_Style_Border::BORDER_THIN
				),
				'top' => array(
					'style' => PHPExcel_Style_Border::BORDER_THIN
				),
				'left' => array(
					'style' => PHPExcel_Style_Border::BORDER_THIN
				),
				'right' => array(
					'style' => PHPExcel_Style_Border::BORDER_THIN
				)
			)
		);
		
		#Cria um novo objecto PHPExcel
		$objPHPExcel = new PHPExcel();
		#Define as propriedades do documento
		$objPHPExcel->getProperties()->setCreator("BiostrumentStocks");
		$objPHPExcel->getProperties()->setLastModifiedBy("BiostrumentStocks");
		$objPHPExcel->getProperties()->setTitle("Loja");
		$objPHPExcel->getProperties()->setSubject("Loja");
		$objPHPExcel->getProperties()->setDescription("Loja");
		$objPHPExcel->getProperties()->setKeywords("Loja");
		$objPHPExcel->getProperties()->setCategory("Loja");
		
		#Remove as gridlines
		$objPHPExcel->getActiveSheet()->setShowGridlines(true);
		#Atribui um Header e um Footer para a impressão
		$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&L&BProdutos da Loja&RImpresso em &D');
		$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddFooter('&L&B' . $objPHPExcel->getProperties()->getTitle() . '&RPágina &P de &N');
		#Define o tamanho e orientação da página
		$objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
		$objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
		
		#Cria uma folha de dados
		$objPHPExcel->setActiveSheetIndex(0);
		#Modifica o nome da folha de dados
		$objPHPExcel->getActiveSheet()->setTitle('Produtos da Loja');
		#Retorna o objecto PHPExcel
		
		$categorias = Doctrine_Query::create()
							->from("Categorias c")
							->innerJoin("c.Produtos p")
							->innerJoin("p.ProdutosLojas pl")
							->where("pl.loja_id = ?", array($this->loja_id))->fetchArray();
		$i = 1;
		foreach ($categorias as $categoria){
			$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow(0, $i)->applyFromArray($estiloCategorias);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $i, $categoria['nome']);
			$i++;
			foreach ($categoria['Produtos'] as $produto) {
				$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow(0, $i)->applyFromArray($estiloNormal);
				$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $i, $produto['nome']);
				$i++;
			}
		}
		
		return $objPHPExcel;
	}

	/**
	 * Método herdado da Interface iExportExcel
	 *
	 */
	public function getNomeFicheiro () {
		$categoria = Doctrine_Query::create()->from("Lojas l")
					->where("l.id = ?", array($this->loja_id))->execute(array(), Doctrine::HYDRATE_ARRAY);
		$nome = str_replace(" ", "", $categoria[0]['nome']);
		return "loja_".$nome."_".$categoria[0]['id']."_".date("[dmY_His]").".xls";
	}
}
?>